<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4">
        <TreeSelect
          :treeData="treeData"
          @nodeClick="nodeClick"
          title="重大危险源结构"
        ></TreeSelect>
      </el-col>
      <el-col :span="20">
        <SearchForm
          v-show="showSearch"
          v-model="queryParams"
          :options="options.formOps"
          @onQuery="getList"
        ></SearchForm>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd"
              >新增</el-button
            >
          </el-col>

          <right-toolbar
            v-model:showSearch="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>
        <Table
          ref="table"
          :auto-featch="true"
          :fetch-data="fetchData"
          :options="options.table"
        >
          <el-table-column
            label="文件名称"
            align="center"
            key="fileName"
            prop="fileName"
          />
          <el-table-column
            label="文件上传时间"
            align="center"
            key="createTime"
            prop="createTime"
          />
          <el-table-column
            label="创建人"
            align="center"
            key="createUserName"
            prop="createUserName"
          />
          <template #actionButton="{ row }">
            <el-button type="text" icon="download" @click="handDownload(row)"
              >下载</el-button
            >
            <el-button type="text" icon="Edit" @click="handleEdit(row)"
              >修改</el-button
            >

            <el-button
              type="text"
              icon="Delete"
              style="color: red"
              @click="handleDel(row)"
              >删除</el-button
            >
          </template></Table
        >
        <DialogVform
          :formJson="formJson"
          ref="dialogVform"
          :slots="['fileSolt']"
          @onSubmit="onSubmit"
          :title="dialogType == 'add' ? '新增危险源档案' : '修改危险源档案'"
        >
          <template #fileSolt>
            <el-row>
              <el-col :span="24">
                <el-form-item label="文件名称" prop="fileName">
                  <el-input
                    v-model="form.fileName"
                    placeholder="上传文件后自动填写"
                    maxlength="30"
                    disabled
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item
                  label="文件上传"
                  prop="fileId"
                  label-position="right"
                >
                  <FileUpload
                    :limit="1"
                    :fileSize="30"
                    :fileType="['doc', 'docx', 'pdf']"
                    v-model="files"
                  ></FileUpload>
                </el-form-item>
              </el-col>
            </el-row>
          </template> </DialogVform
      ></el-col>
    </el-row>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import { qingdanNode as APIS, qingdan } from "@/api/hdanger/qingdan/index.js";
import formJson from "./components/options.json";
import FileUpload from "@/components/FileUpload";
import TreeSelect from "@/components/SideTree";
import { useAsyncData, useFormJson } from "@/views/hooks/useAsyncData.js";

const treeData = ref([]);
const { proxy } = getCurrentInstance();
const ops = useFormJson(formJson, {
  sort: [],
  sortable: [],
});
const options = reactive({
  table: ops,
  formOps: ops.filter((i) =>
    ["dangerNodeName", "dangerLevel"].includes(i.prop)
  ),
});
const showSearch = ref(true);
const dialogVform = ref();
const files = ref([]);

function nodeClick(data) {
  form.value.dangerId = data.key;
  queryParams.value.dangerId = data.key;
  getList();
}
watch(
  () => files,
  (newData) => {
    console.log(newData);
    if (newData.value.length === 0) {
      form.value.fileName = "";
      form.value.fileId = "";
      form.value.fileUrl = "";
    } else {
      form.value.fileName = newData.value[0].name;
      form.value.fileId = newData.value[0].id;
      form.value.fileUrl = newData.value[0].url;
    }
  },
  {
    deep: true,
  }
);
const handDownload = (row) => {
  window.downLoadFile(row.fileUrl, row.fileName);
};

const plugEdit = (row) => {
  if (row.fileId) {
    files.value = [
      {
        name: row.fileName,
        id: row.fileId,
        url: row.fileUrl,
      },
    ];
  } else {
    files.value = [];
  }
};

const table = ref();

onMounted(() => {
  qingdan
    .query({
      pageNum: 1,
      pageSize: 99999,
    })
    .then((res) => {
      treeData.value = [
        {
          label: "全部",
          id: "",
          children: res.rows.map((ele) => ({
            ...ele,
            children: [],
            label: ele.dangerName,
          })),
        },
      ];
      console.log(treeData);
    });
});
const {
  onSubmit,
  fetchData,
  dialogType,
  handleAdd,
  handleEdit,
  getList,
  handleDel,
  form,
  queryParams,
} = useAsyncData({
  APIS,
  formJson,
  dialogVform,
  table,
  proxy,
  plugEdit,
});
</script>

<style lang="scss" scoped>
::v-deep.el-form-item__label {
  text-align: right !important;
}
</style>
